package com.web.servlet;

import com.bean.Disaster;
import com.dao.UserDao;
import com.web.common.exception.DataAccessException;
import com.web.common.utils.BeanFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author wangzhipeng
 * @Date 2020-08-31 9:53 下午
 * @Description
 */
@WebServlet(urlPatterns = "/UploadDisasterServlet")
public class UploadDisasterServlet extends HttpServlet {
    UserDao userDao = (UserDao) BeanFactory.getBean(BeanFactory.USER_DAO);
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
       // String dtupe = new String(request.getParameter("type").getBytes("iso8859-1"),"UTF-8");
        String dtupe = request.getParameter("type");
        System.out.println(dtupe);
       // String dinfo = new String(request.getParameter("info").getBytes("iso8859-1"),"UTF-8");
        String dinfo = request.getParameter("info");
        String dphone = request.getParameter("phone");
      //  String dlocation = new String(request.getParameter("location").getBytes("iso8859-1"),"UTF-8");
        String dlocation = request.getParameter("location");
     //   String ddegree = new String(request.getParameter("degree").getBytes("iso8859-1"),"UTF-8");
        String ddegree  = request.getParameter("degree");
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        System.out.println("提交的用户id："+uid);
        Disaster disaster = new Disaster(null,dtupe,ddegree,dinfo,dlocation,dphone,uid);

        int result = 0;
        try {
            result = userDao.uploadDisaster(disaster);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter = response.getWriter();
        if(result == 1){
            printWriter.println("上传成功！");
        }else {
            printWriter.println("上传失败！");
        }
        printWriter.flush();
        printWriter.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);


    }
}
